package longcaisuyun.longcai.com.yiliubabalongcaisuyun;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.zcx.helper.secret.SecretAESDESede;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import longcaisuyun.longcai.com.mystatic.publicUrl;
import longcaisuyun.longcai.com.myview.MTextView;
import longcaisuyun.longcai.com.myview.MyListView;
import longcaisuyun.longcai.com.suyunbean.CityBean;
import longcaisuyun.longcai.com.suyunbean.ShouFeiFJBean;
import longcaisuyun.longcai.com.suyunbean.ShouFeiup;
import wh.utils.StringUtils;

//                            _ooOoo_
//                           o8888888o
//                           88" . "88
//                           (| -_- |)
//                            O\ = /O
//                        ____/`---'\____
//                      .   ' \\| |// `.
//                       / \\||| : |||// \
//                     / _||||| -:- |||||- \
//                       | | \\\ - /// | |
//                     | \_| ''\---/'' | |
//                      \ .-\__ `-` ___/-. /
//                   ___`. .' /--.--\ `. . __
//                ."" '< `.___\_<|>_/___.' >'"".
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |
//                 \ \ `-. \_ __\ /__ _/ .-` / /
//         ======`-.____`-.___\_____/___.-`____.-'======
//                            `=---='
//
//         .............................................
//         佛祖保佑             永无BUG
//         佛曰:
//                  写字楼里写字间，写字间里程序员；
//                  程序人员写程序，又拿程序换酒钱。
//                  酒醒只在网上坐，酒醉还来网下眠；
//                  酒醉酒醒日复日，网上网下年复年。
//                  但愿老死电脑间，不愿鞠躬老板前；
//                  奔驰宝马贵者趣，公交自行程序员。
//                  别人笑我忒疯癫，我笑自己命太贱；
//                  不见满街漂亮妹，哪个归得程序员？
/************************************************************/
//收费标准

/************************************************************/

public class ShouFeiActivity extends Activity implements View.OnClickListener {
    private AMapLocationClient locationClient = null;
    private AMapLocationClientOption locationOption = null;
    MTextView mt_1, mt_2;
    TextView tv_city;
    MyListView listView4, listView5, listView6;
    LinearLayout lin_fh;
    String id = "";
    List<ShouFeiup> list = new ArrayList();
    List<String> list_guize = new ArrayList<>();
    List<ShouFeiFJBean> list_fj = new ArrayList<>();
    CheXingAdapter c = new CheXingAdapter(ShouFeiActivity.this, list);
    ShoufeiAdapter sa = new ShoufeiAdapter(ShouFeiActivity.this, list_fj);
    ShouFeiGZAdapter sgz = new ShouFeiGZAdapter(ShouFeiActivity.this, list_guize);
    ScrollView sc;
    private String address;
    private String areaId;
    private List<CityBean> listCity=new ArrayList<>();
    private String[] shi;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shou_fei);
        View view = getWindow().getDecorView();
        // 适配工具将视图重新排布
        MyApplication.ScaleScreenHelper.loadView((ViewGroup) view);


        ViewInit();
        //调用方法显示当前数据
        httpget();
        DataInit();

    }


    private void DataInit() {
        listView4.setAdapter(c);
        listView5.setAdapter(sa);
        listView6.setAdapter(sgz);
    }

    private void ViewInit() {
        tv_city = (TextView) findViewById(R.id.tv_city);
        lin_fh = (LinearLayout) findViewById(R.id.lin_fh);
        lin_fh.setOnClickListener(this);
        tv_city.setOnClickListener(this);
        listView4 = (MyListView) findViewById(R.id.listView4);
        listView5 = (MyListView) findViewById(R.id.listView5);
        listView6 = (MyListView) findViewById(R.id.listView6);
        sc = (ScrollView) findViewById(R.id.sc);
        //得到地址
        address=MyApplication.myPreferences.readAddress();
        if (!address.contains("省")){

        }else{
            String[] a= address.split("省");
            shi=a[1].split("市");
            tv_city.setText(shi[0]+"市");
        }
//        Log.e("现在的地址",address);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.lin_fh:
                ShouFeiActivity.this.finish();
                break;
            case R.id.tv_city:
                Intent intent = new Intent(ShouFeiActivity.this, CityAcitvity.class);
                startActivityForResult(intent, 0);
                break;
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0 && resultCode == 1) {
            id = data.getStringExtra("id");
            httpPost(id);
            tv_city.setText(data.getStringExtra("content"));
        }
    }

    private void httpPost(String cityid) {
        final SecretAESDESede secretAESDESede = new SecretAESDESede("123456781234567812345678", "12345678", SecretAESDESede.DESEDE_CBC_PKCS5PADDING);
        final FinalHttp finalHttp = new FinalHttp();
        AjaxParams params = new AjaxParams();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("areaid", cityid);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            params.put("Keys", secretAESDESede.encrypt(jsonObject.toString()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        finalHttp.post(publicUrl.shoufei, params, new AjaxCallBack<String>() {
            public void onStart() {
                super.onStart();
                MyApplication.myviewutil.startProgressDialog(ShouFeiActivity.this, finalHttp, publicUrl.mobilecode);
            }

            public void onSuccess(String s) {
                super.onSuccess(s);

                try {
                    JSONObject jsonObject = null;
                    try {
                        jsonObject = new JSONObject(secretAESDESede.decrypt(s));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    Log.e("jsonObject", jsonObject.toString());
                    String Message = jsonObject.getString("message");
                    if (Message.equals("1")) {
                        sc.setVisibility(View.VISIBLE);
                        addlistup(jsonObject.getJSONArray("list"));
                        c.notifyDataSetChanged();
                        addGuiZe(jsonObject.getJSONArray("infolist"));
                        sgz.notifyDataSetChanged();
                        adddowm(jsonObject.getJSONArray("addlist"));
                        sa.notifyDataSetChanged();
                    }
                    if (Message.equals("0")) {
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                MyApplication.myviewutil.stopProgressDialog();
            }

            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
                MyApplication.myviewutil.stopProgressDialog();
            }
        });
    }

    public void addlistup(JSONArray jsonArray) {
        list.clear();
        for (int j = 0; j < jsonArray.length(); j++) {
            ShouFeiup shouFeiup = new ShouFeiup();
            try {
                JSONObject jsonObject = jsonArray.getJSONObject(j);
                shouFeiup.setTitle(jsonObject.getString("title"));
                shouFeiup.setCosts(jsonObject.getString("costs"));
                shouFeiup.setPrice(jsonObject.getString("price"));

                list.add(shouFeiup);
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }

    public void addGuiZe(JSONArray jsonArray) {
        list_guize.clear();
        for (int j = 0; j < jsonArray.length(); j++) {
            try {
                JSONObject jsonObject = jsonArray.getJSONObject(j);
                String s = jsonObject.getString("title");
                list_guize.add(s);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    public void adddowm(JSONArray jsonArray) {
        list_fj.clear();
        for (int j = 0; j < jsonArray.length(); j++) {
            try {
                JSONObject jsonObject = jsonArray.getJSONObject(j);
                ShouFeiFJBean shouFeiFJBean = new ShouFeiFJBean();
                shouFeiFJBean.setTitle(jsonObject.getString("title"));
                shouFeiFJBean.setInfo(jsonObject.getString("info"));
                list_fj.add(shouFeiFJBean);
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }
    public void httpget() {
        final FinalHttp finalHttp = new FinalHttp();
        finalHttp.get(publicUrl.city, new AjaxCallBack<String>() {

            public void onStart() {
                super.onStart();
                MyApplication.myviewutil.startProgressDialog(ShouFeiActivity.this, finalHttp, publicUrl.city);

            }

            public void onSuccess(String s) {
                super.onSuccess(s);
                try {
                    JSONObject jsonObject = new JSONObject(s);
                    String message = jsonObject.getString("message");
                    JSONArray jsonArray = jsonObject.getJSONArray("list");
                    tolist(jsonArray);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                MyApplication.myviewutil.stopProgressDialog();
            }

            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
                MyApplication.myviewutil.stopProgressDialog();
                Toast.makeText(ShouFeiActivity.this, "网络故障,请稍后重试", Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void tolist(JSONArray jsonArray) {
        for (int j = 0; j < jsonArray.length(); j++) {
            try {
                JSONObject jsonObject = (JSONObject) jsonArray.get(j);
                CityBean cityBean = new CityBean();

                cityBean.setAreaid(jsonObject.getString("areaid"));
                cityBean.setClassname(jsonObject.getString("classname"));
                listCity.add(cityBean);
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
        if (!address.contains("省")){

        }else{
            for (int i = 0; i < listCity.size(); i++) {
                if (listCity.get(i).getClassname().equals(shi[0]+"市")){
                    areaId= listCity.get(i).getAreaid();
                }
            }
            if (!StringUtils.isEmpty(areaId)){
                httpPost(areaId);
            }
        }
    }
}
